package com.xjrsoft.module.customerTwo.ycode.deal_standard_data;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.google.gson.Gson;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.dto.TYcodeClassListDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.entity.TYcodeClass;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_class.service.ITYcodeClassService;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.entity.TYcodeMasterDataStandardDetail;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.entity.TYcodeMasterDataStandardDetailEnum;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.service.ITYcodeMasterDataStandardDetailEnumService;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_master_data_standard_detail.service.ITYcodeMasterDataStandardDetailService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2024-07-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/deal_standard_data")
@Api(value = "分类管理", tags = "分类管理接口")
@Slf4j
public class DealStandardDataController {

    private ITYcodeClassService tYcodeClassService;
    private ITYcodeMasterDataStandardDetailService tYcodeMasterDataStandardDetailService;
    private ITYcodeMasterDataStandardDetailEnumService tYcodeMasterDataStandardDetailEnumService;

    /**
     * 将主数据标准的父节点下所有明细复制到子节点
     */
    @GetMapping("/dealData")
    public Response dealData(@RequestParam("standardId") String standardId) {
        TYcodeClass tYcodeClass = tYcodeClassService.getById(standardId);
        if(null == tYcodeClass){
            return Response.notOk("父节点id有误，查询不到该分类");
        }
        //查询其下所有子节点
        TYcodeClassListDto dto = new TYcodeClassListDto();
        dto.setParent_code(tYcodeClass.getCompleteCode());
        List<TYcodeClass> childList = tYcodeClassService.getList(dto);
        if(CollectionUtils.isEmpty(childList)){
            return Response.notOk("该分类下没有子节点");
        }
        //查询其下所有标准明细
        List<TYcodeMasterDataStandardDetail> detailList = tYcodeMasterDataStandardDetailService.getDetailListBystandardId(standardId);
        if(CollectionUtils.isEmpty(detailList)){
            return Response.notOk("该分类没有标准明细");
        }
        List<TYcodeMasterDataStandardDetail> addDetail = new ArrayList<>();
        List<TYcodeMasterDataStandardDetailEnum> addEnum = new ArrayList<>();
        for(TYcodeClass child : childList){
            for(TYcodeMasterDataStandardDetail detail : detailList){
                TYcodeMasterDataStandardDetail detailTemp = BeanUtil.copy(detail,TYcodeMasterDataStandardDetail.class);
                List<TYcodeMasterDataStandardDetailEnum> enumList = tYcodeMasterDataStandardDetailService.getTYcodeMasterDataStandardDetailEnumByParentId(detail.getId());
                String detailId = IdWorker.get32UUID();
                if(CollectionUtil.isNotEmpty(enumList)){
                    for(TYcodeMasterDataStandardDetailEnum one : enumList){
                        TYcodeMasterDataStandardDetailEnum enumTemp = BeanUtil.copy(one,TYcodeMasterDataStandardDetailEnum.class);
                        enumTemp.setId(IdWorker.get32UUID());
                        enumTemp.setDetailId(detailId);
                        addEnum.add(enumTemp);
                    }
                }
                detailTemp.setId(detailId);
                detailTemp.setStandardId(child.getId());
                addDetail.add(detailTemp);
            }
        }
        log.error("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        log.error(new Gson().toJson(addDetail));
        log.error("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        return Response.status(tYcodeMasterDataStandardDetailService.saveBatch(addDetail)&&tYcodeMasterDataStandardDetailEnumService.saveBatch(addEnum));
    }
}
